package br.edu.planodesaude.GUI.procedimento;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;

import br.edu.planodesaude.controle.Cadastradora;
import br.edu.planodesaude.controle.Pesquisadora;
import br.edu.planodesaude.dominio.Especialidade;
import br.edu.planodesaude.exceptions.InsertionException;
import br.edu.planodesaude.exceptions.SearchException;
import br.edu.planodesaude.util.Conexao;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author gustavo
 */
public class CadastrarProcedimento extends javax.swing.JInternalFrame {

	/**
	 * Creates new form CadastrarProcedimento
	 */
	public CadastrarProcedimento() {
		setLocation((int)JDesktopPane.CENTER_ALIGNMENT, (int)JDesktopPane.CENTER_ALIGNMENT);
		setClosable(true);
		setVisible(true);
		initComponents();
		setTitle("Cadastrar Procedimento");

		Object[] items = null;

		try {
			items = new Pesquisadora().getEspecialidades().toArray();

		} catch (SearchException e) {
			JOptionPane.showMessageDialog(null, e.getMessage(), "Alerta", JOptionPane.WARNING_MESSAGE);
		}

		DefaultComboBoxModel model = new DefaultComboBoxModel(items);

		especialidadesJComboBox.setModel(model);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		buttonGroup1 = new javax.swing.ButtonGroup();
		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		nomeJTextField = new javax.swing.JTextField();
		precoJTextField = new javax.swing.JTextField();
		especialidadesJComboBox = new javax.swing.JComboBox();
		consultaJRadioButton = new javax.swing.JRadioButton();
		cirurgiaJRadioButton = new javax.swing.JRadioButton();
		jButton1 = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		jLabel1.setText("Nome");

		jLabel2.setText("Preço");

		jLabel3.setText("Especialidade");

		jLabel4.setText("Tipo");

		especialidadesJComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2",
				"Item 3", "Item 4" }));

		buttonGroup1.add(consultaJRadioButton);
		consultaJRadioButton.setText("Consulta");

		buttonGroup1.add(cirurgiaJRadioButton);
		cirurgiaJRadioButton.setText("Cirurgia");

		jButton1.setText("Cadastrar");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGroup(
										layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addGap(29, 29, 29)
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addGroup(
																						layout.createSequentialGroup()
																								.addGroup(
																										layout.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.LEADING)
																												.addComponent(
																														jLabel1)
																												.addComponent(
																														jLabel2))
																								.addGap(63, 63, 63)
																								.addGroup(
																										layout.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.LEADING)
																												.addComponent(
																														nomeJTextField,
																														javax.swing.GroupLayout.PREFERRED_SIZE,
																														332,
																														javax.swing.GroupLayout.PREFERRED_SIZE)
																												.addComponent(
																														precoJTextField,
																														javax.swing.GroupLayout.PREFERRED_SIZE,
																														120,
																														javax.swing.GroupLayout.PREFERRED_SIZE)))
																				.addGroup(
																						layout.createSequentialGroup()
																								.addGroup(
																										layout.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.LEADING)
																												.addComponent(
																														jLabel3)
																												.addComponent(
																														jLabel4))
																								.addGroup(
																										layout.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.LEADING)
																												.addGroup(
																														layout.createSequentialGroup()
																																.addPreferredGap(
																																		javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																																.addGroup(
																																		layout.createParallelGroup(
																																				javax.swing.GroupLayout.Alignment.LEADING)
																																				.addComponent(
																																						consultaJRadioButton)
																																				.addComponent(
																																						cirurgiaJRadioButton)))
																												.addGroup(
																														layout.createSequentialGroup()
																																.addGap(10,
																																		10,
																																		10)
																																.addComponent(
																																		especialidadesJComboBox,
																																		javax.swing.GroupLayout.PREFERRED_SIZE,
																																		250,
																																		javax.swing.GroupLayout.PREFERRED_SIZE))))))
												.addGroup(
														layout.createSequentialGroup()
																.addGap(190, 190, 190)
																.addComponent(jButton1,
																		javax.swing.GroupLayout.PREFERRED_SIZE, 103,
																		javax.swing.GroupLayout.PREFERRED_SIZE)))
								.addContainerGap(34, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGap(24, 24, 24)
								.addGroup(
										layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
												.addGroup(
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.TRAILING)
																				.addComponent(jLabel1)
																				.addComponent(
																						nomeJTextField,
																						javax.swing.GroupLayout.PREFERRED_SIZE,
																						javax.swing.GroupLayout.DEFAULT_SIZE,
																						javax.swing.GroupLayout.PREFERRED_SIZE))
																.addGap(18, 18, 18).addComponent(jLabel2))
												.addComponent(precoJTextField, javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel3)
												.addComponent(especialidadesJComboBox,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel4).addComponent(consultaJRadioButton))
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addComponent(cirurgiaJRadioButton).addGap(18, 18, 18).addComponent(jButton1)
								.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

		setBounds(439, 224, 480, 270);
	}// </editor-fold>

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
		if(nomeJTextField.getText().equals("")){
			JOptionPane.showMessageDialog(null, "Campo Nome obrigatório!", "Alerta", JOptionPane.WARNING_MESSAGE);
		}else if(precoJTextField.getText().equals("")){
			JOptionPane.showMessageDialog(null, "Campo Preço obrigatório!", "Alerta", JOptionPane.WARNING_MESSAGE);
		}else if(buttonGroup1.getSelection() == null){
			JOptionPane.showMessageDialog(null, "Selecione o tipo do procedimento!", "Alerta", JOptionPane.WARNING_MESSAGE);
		}else{
			float preco;
			try{
				preco = Float.parseFloat(precoJTextField.getText());
				new Cadastradora().cadastrarProcedimento(nomeJTextField.getText(), preco, cirurgiaJRadioButton.isSelected() ? "cirurgia" : "consulta", (Especialidade)especialidadesJComboBox.getSelectedItem());;
				JOptionPane.showMessageDialog(null, "Cadastro Realizado com sucesso!", "Sucesso!", JOptionPane.INFORMATION_MESSAGE);
				dispose();
			}catch(NumberFormatException nfex){
				JOptionPane.showMessageDialog(null, "Preço inválido!", "Erro", JOptionPane.ERROR_MESSAGE);
			}catch (InsertionException e) {
				JOptionPane.showMessageDialog(null, e.getMessage(), "Erro", JOptionPane.WARNING_MESSAGE);
			}
		}
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		// <editor-fold defaultstate="collapsed"
		// desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase
		 * /tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(CadastrarProcedimento.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(CadastrarProcedimento.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(CadastrarProcedimento.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(CadastrarProcedimento.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		}
		// </editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new CadastrarProcedimento().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify
	private javax.swing.ButtonGroup buttonGroup1;
	private javax.swing.JRadioButton cirurgiaJRadioButton;
	private javax.swing.JRadioButton consultaJRadioButton;
	private javax.swing.JComboBox especialidadesJComboBox;
	private javax.swing.JButton jButton1;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JTextField nomeJTextField;
	private javax.swing.JTextField precoJTextField;
	// End of variables declaration
}
